﻿Imports System
Imports System.Collections
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend Class DebuggerHelpers
        ' Methods
        Private Shared Function GetActivity(ByVal containerActivity As Activity, ByVal id As String) As Activity
            If (Not containerActivity Is Nothing) Then
                Dim queue As New Queue
                queue.Enqueue(containerActivity)
                Do While (queue.Count > 0)
                    Dim activity As Activity = DirectCast(queue.Dequeue, Activity)
                    If activity.Enabled Then
                        If (activity.QualifiedName = id) Then
                            Return activity
                        End If
                        If TypeOf activity Is CompositeActivity Then
                            Dim activity2 As Activity
                            For Each activity2 In DirectCast(activity, CompositeActivity).Activities
                                queue.Enqueue(activity2)
                            Next
                        End If
                    End If
                Loop
            End If
            Return Nothing
        End Function

        Private Shared Function IsDeclaringActivityMatchesContext(ByVal currentActivity As Activity, ByVal context As Activity) As Boolean
            Dim compositeActivity As CompositeActivity = [TryCast](context,CompositeActivity)
            Dim declaringActivity As CompositeActivity = Helpers.GetDeclaringActivity(currentActivity)
            If (Helpers.IsActivityLocked(context) AndAlso ((compositeActivity Is Nothing) OrElse Not Helpers.IsCustomActivity(compositeActivity))) Then
                compositeActivity = Helpers.GetDeclaringActivity(context)
            End If
            Return (compositeActivity Is declaringActivity)
        End Function

        Friend Shared Function ParseActivity(ByVal parsingContext As Activity, ByVal activityName As String) As Activity
            If (parsingContext Is Nothing) Then
                Throw New ArgumentNullException("parsingContext")
            End If
            If (activityName Is Nothing) Then
                Throw New ArgumentNullException("activityName")
            End If
            Dim id As String = activityName
            Dim str2 As String = String.Empty
            Dim index As Integer = activityName.IndexOf(".")
            If (index <> -1) Then
                id = activityName.Substring(0, index)
                str2 = activityName.Substring((index + 1))
                If (str2.Length = 0) Then
                    Return Nothing
                End If
            End If
            Dim containerActivity As Activity = Nothing
            containerActivity = DebuggerHelpers.GetActivity(parsingContext, id)
            If (((containerActivity Is Nothing) AndAlso TypeOf parsingContext Is CompositeActivity) AndAlso ((Not parsingContext.Parent Is Nothing) AndAlso Helpers.IsCustomActivity([TryCast](parsingContext,CompositeActivity)))) Then
                containerActivity = DebuggerHelpers.GetActivity(parsingContext, (parsingContext.QualifiedName & "." & id))
            End If
            If (containerActivity Is Nothing) Then
                Return Nothing
            End If
            If (str2.Length > 0) Then
                If (Not TypeOf containerActivity Is CompositeActivity OrElse Not Helpers.IsCustomActivity([TryCast](containerActivity,CompositeActivity))) Then
                    Return Nothing
                End If
                Dim strArray As String() = str2.Split(New Char() { "."c })
                Dim i As Integer
                For i = 0 To strArray.Length - 1
                    Dim activity As Activity = DebuggerHelpers.GetActivity(containerActivity, (containerActivity.QualifiedName & "." & strArray(i)))
                    If ((activity Is Nothing) OrElse Not Helpers.IsActivityLocked(activity)) Then
                        Return Nothing
                    End If
                    Dim declaringActivity As CompositeActivity = Helpers.GetDeclaringActivity(activity)
                    If (Not containerActivity Is declaringActivity) Then
                        Return Nothing
                    End If
                    containerActivity = activity
                Next i
                Return containerActivity
            End If
            If (Helpers.IsActivityLocked(containerActivity) AndAlso Not DebuggerHelpers.IsDeclaringActivityMatchesContext(containerActivity, parsingContext)) Then
                Return Nothing
            End If
            Return containerActivity
        End Function

    End Class
End Namespace

